package com.elmcode.study.controller;

import com.elmcode.study.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Api(tags = "文件接口")
@RestController
@RequestMapping("/file")
public class FileController {

    @PostMapping("/upload")
    @ApiOperation("文件上传接口")
    public Result upload(String name, MultipartFile header){
        //header要和<input type="file" name="header"
        //header对象就包含了<input type="file" name="header"所有信息

        System.out.println(header.getSize());
        System.out.println(header.getOriginalFilename());
        System.out.println(header.getName());
        System.out.println(header.getContentType());

        //保存
        try {
            header.transferTo(new File("E:\\实训\\实训2\\笔记\\day02\\"+header.getOriginalFilename()));

            return Result.ok("成功,name="+name);
        } catch (IOException ioException) {
            ioException.printStackTrace();

            return Result.ok("失败",500);
        }
    }
}